package com.china08.yxyapi.api.manage.sch.platform.users.teacher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonItemRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLessonsTeachingTopicItemCrtController extends YxyApiBaseController {

    @Autowired
    YxyLessonsTopicLessonItemRepository itemRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/lessons/tea/item/create.e", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.lessonId;
        String chapterId = reqModel.chapterId;
        String title = reqModel.title;
        String intro = reqModel.intro;
        String videoId = reqModel.videoId;
        Integer status = reqModel.status;
        Integer preview = reqModel.preview;

        Integer orderNum = itemRepository.countByLessonId(lessonId) + 1;
        YxyLessonsTopicLessonItem item = new YxyLessonsTopicLessonItem();
        item.setLessonId(lessonId);
        item.setChapterId(chapterId);
        item.setTitle(title);
        item.setIntro(intro);
        item.setStatus(status);
        item.setPreview(preview);
        item.setVideoId(videoId);
        item.setOrderNum(orderNum);
        itemRepository.save(item);
        return null;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;
        public String chapterId;
        public String title;
        public String intro;
        public Integer status = 0;
        public Integer preview = 0;
        public String videoId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(lessonId), "未获取到当前课程");
            Assert.isTrue(StringUtils.isNotBlank(chapterId), "未获取到当前章节");
            Assert.isTrue(StringUtils.isNotBlank(title), "标题不能为空");
            Assert.isTrue(title.length() > 0 && title.length() < 36, "请输入1-35个字的课时名称");
            Assert.isTrue(StringUtils.isNotBlank(intro), "简介不能为空");
            Assert.isTrue(intro.length() > 0 && intro.length() < 501, "请输入1-500字的简介");
            Assert.isTrue(StringUtils.isNotBlank(videoId), "未获取到视频不能为空");
        }
    }

    public static class RespModel extends YxyApiRespModel {
    }
}
